package com.liangtian.mdm.system.parameter.service.imp;

import com.liangtian.mdm.system.parameter.dao.SysParameterDao;
import com.liangtian.mdm.system.parameter.model.SysParameter;
import com.liangtian.mdm.system.parameter.service.SysParameterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("SysParameterService")
public class SysParameterServiceImp implements SysParameterService {

    @Autowired
    private SysParameterDao sysParameterDao;

    @Override
    public PageInfo queryPage(Map<String, Object> map) {

        PageInfo<List<SysParameter>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<SysParameter> sysParameterList = sysParameterDao.querySysParameterList(map);

            pageInfo = new PageInfo(sysParameterList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }


    @Override
    public int addSysParameter(SysParameter sysParameter) {

        int flag = 0;
        //校验名称重复 1 :名称重复
        int flagName = sysParameterDao.addSysParameterCheckName(sysParameter);
        if (flagName > 0) {
            flag = 1;
            return flag;
        } else {
            sysParameterDao.addSysParameter(sysParameter);
            return Integer.valueOf(sysParameter.getParameterId());
        }


    }


    @Override
    public SysParameter getSysParamById(SysParameter sysParameter) {
        return sysParameterDao.getSysParamById(sysParameter);
    }

    @Override
    public int editSysParam(SysParameter sysParameter) {
        return sysParameterDao.editSysParam(sysParameter);

    }


    @Override
    public int delSysParamByIds(Map<String, Object> map) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public int updateSysParamStatus(Map<String, Object> map) {
        return sysParameterDao.updateSysParamStatus(map);
    }


    @Override
    public String getSysParamValue(String sysParam, String defaultValue) {

        String paramValue = sysParameterDao.getSysParamValue(sysParam);

        if (null == paramValue || paramValue.equalsIgnoreCase(""))
            return defaultValue;
        return paramValue;

    }


    @Override
    public List<SysParameter> getParameterLst() {
        return sysParameterDao.getParameterLst();
    }


}
